package com.zq.zqplayer.mvp.main.ui

import android.app.job.JobInfo
import android.app.job.JobInfo.NETWORK_TYPE_ANY
import android.app.job.JobScheduler
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.os.PersistableBundle
import android.view.View
import androidx.annotation.RequiresApi
import androidx.work.*
import com.ziq.base.base.BaseFragment
import com.ziq.base.extension.binding
import com.ziq.base.extension.click
import com.zq.zqplayer.R
import com.zq.zqplayer.databinding.FragmentMineBinding
import com.zq.zqplayer.test.JobSchedulerService
import com.zq.zqplayer.test.TestListActivity
import com.zq.zqplayer.test.TestWorker
import java.time.Duration


class MineFragment : BaseFragment(R.layout.fragment_mine) {

    private val binding : FragmentMineBinding by binding()

    @RequiresApi(Build.VERSION_CODES.O)
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        binding.floatingBtn.click {
            startActivity(Intent(activity, TestListActivity::class.java))
        }

        val mJobScheduler: JobScheduler? =
            context?.getSystemService(Context.JOB_SCHEDULER_SERVICE) as JobScheduler?
        binding.job1.click {
            val JOB_ID = 100;
            val build: JobInfo.Builder  = JobInfo.Builder(JOB_ID, ComponentName(requireContext(), JobSchedulerService::class.java))
            build.setRequiredNetworkType(NETWORK_TYPE_ANY)
            build.setExtras(PersistableBundle().apply {
                putString("job-key", "job-100-value")
            })
            val jobInfo = build.build()
            mJobScheduler?.schedule(jobInfo)
        }
        binding.job2.click {
            val JOB_ID = 200;
            val build: JobInfo.Builder  = JobInfo.Builder(JOB_ID, ComponentName(requireContext(), JobSchedulerService::class.java))
//            build.setRequiredNetworkType(NETWORK_TYPE_ANY)
            build.setExtras(PersistableBundle().apply {
                putString("job-key", "job-200-value")
            })
            val jobInfo = build.build()
            mJobScheduler?.schedule(jobInfo)
        }

        binding.jobCancel.click {
            mJobScheduler?.cancelAll()
        }

        binding.worker.click {
            val myConstraints: Constraints = Constraints.Builder()
                .setRequiredNetworkType(NetworkType.CONNECTED)
                .build()
            val data = Data.Builder()
                .putString("key1", "value1")
                .build();
            val workRequest: WorkRequest =
                PeriodicWorkRequest.Builder(TestWorker::class.java, Duration.ofMinutes(15))
                    .setInputData(data)
                    .setConstraints(myConstraints)
                    .build()
            WorkManager
                .getInstance(requireContext())
                .enqueue(workRequest)
        }

    }


}